package model;

//import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;   // important
//import javax.mail.event.*;  // important
//import java.net.*;
import java.security.Security;
import java.util.*;

public class ShareWithMeMail {
		
	public boolean SendMail()
	{
		try
		{
			Properties props=new Properties();
	
			
	
			Session session1 = Session.getDefaultInstance(props,null);
			String fromEmail = "khunnathawat@gmail.com";	
			String toEmail = "khunnathawat@gmail.com";	
			String subject ="Test subject";	
			String mailMessage = "test mmail message";
	
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
			
			//props.put("mail.smtp.host","mail.google.com");   //  'localhost' for testing
			props.setProperty("mail.transport.protocol", "smtp");
			props.setProperty("mail.host", "smtp.gmail.com");

			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.port", "465");

			props.put("mail.debug", "true");
			props.put("mail.smtp.socketFactory.port", "465");

			props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.socketFactory.fallback", "false");

			Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() 
			{ 
			protected PasswordAuthentication getPasswordAuthentication() 
			{
				return new PasswordAuthentication("sharewithmeproject@gmail.com","sharewithme@nida");
			}
			});
			
			
			Message message =new MimeMessage(session1);
	
			message.setFrom(new InternetAddress(fromEmail));
	
			message.setRecipients
	
			(Message.RecipientType.TO,InternetAddress.parse(toEmail,false));
	
			message.setSubject(subject);	
			message.setText(mailMessage);  	
			
			Transport.send(message);
			System.out.println("mail has been sent");
			
			return true;

		}
		catch(Exception ex)
		{
			System.out.println("\n\n ERROR....."+ex);
			return false;
		}
	}
}
